<script>

    DEBUG = true;
    window.onerror=function(){
        if(DEBUG){
            alert(JSON.stringify(arguments));
        }
    };
    //录音处理
    var audio_name = "_doc/audio/input.amr";
    // H5 plus事件处理
    function plusReady(){
        log.log("plusReady");
    }
    if(window.plus){
        plusReady();
    }else{
        document.addEventListener('plusready', plusReady, false);
    }
    // 开始录音
    var r=null,t=0,ri=null,rt=null;
    function startRecord(){
        log.log("startRecord");
        r = plus.audio.getRecorder();
        if ( r == null ) {
            outLine('录音对象未获取');
            return;
        }
        r.record({filename:audio_name}, function(p){
            outLine('录音完成：'+p);
            plus.io.resolveLocalFileSystemURL(p, function(entry){
                /////////
                entry.file(function(file) {
                        reader = new plus.io.FileReader();
                        reader.onloadend = function(e) {
                            outLine(e.target.result);
                            audio_str = encodeURI(e.target.result) ;
                            upload_audio(audio_str);
                        };
                        reader.readAsDataURL(file);
                    },
                    function(e) {
                        outLine(e.message);
                    });
                //////////
            }, function(e){
                outLine('读取录音文件错误：'+e.message);
            });
        }, function(e){
            outLine('录音失败：'+e.message);
        } );
    }
    // 停止录音
    function stopRecord(){
        log.log("stopRecord");
        clearInterval(ri);
        ri = null;
        r.stop();
        w = null;
        r = null;
    }


    function upload_audio(audio_str) {
        var api_url = "http://api.xfyun.cn/v1/service/v1/iat";
        var wt = plus.nativeUI.showWaiting();
        var task = plus.uploader.createUpload(api_url, {
                method: "POST"
            },
            function(resp, status) { //上传完成
                wt.close();
                if(status == 200){
                    var resp_text = resp.responseText;
                    outLine(resp_text);
                    var data = JSON.parse(resp_text);
                    successCB(data);
                }else{
                    outLine("status=>"+status+JSON.stringify(resp));
                }
            }
        );
        task.setRequestHeader('engine_type','sms8k');
        task.setRequestHeader('aue','raw');
        task.addData( "audio", audio_str );
        task.addFile( audio_name, {key:"audio"} );
        task.start();
    }

    function successCB(data){
        var msg = data.msg;
    }
    //事件处理
    var div = document.getElementById("push_me");
    var log = document.getElementById("log");
    log.log=function(msg){
        var myDate = new Date();
        var date= myDate.toLocaleString();
        if(DEBUG){
            if(typeof msg != "string"){
                msg = JSON.stringify(msg);
            }
            log.innerHTML = date+"<br/>"+msg+"<br/>"+log.innerHTML;
        }
    };
    outLine = log.log;
    function ondown(){
        log.log("ondown");
        startRecord();
    }
    function onup(){
        stopRecord();
        log.log("onup");
    }
    div.ontouchstart = ondown;
    div.ontouchend  = onup ;
    div.onmousedown = ondown;
    div.onmouseup  = onup ;

</script>